/**********************************************************************
 * FILE : JMSWorker.java
 * CREATE DATE : 2011-11-24
 * DESCRIPTION :
 * CHANGE HISTORY LOG
 * ---------------------------------------------------------------------
 * NO.| DATE | NAME | REASON | DESCRIPTION
 * ---------------------------------------------------------------------
 * 1 | 2011-11-24 | Sting | 创建草稿版本
 * ---------------------------------------------------------------------
 */
package com.sgm.de.benchmark;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

import com.alibaba.fastjson.JSON;
import com.sgm.de.benchmark.act.GlobalAct;
import com.sgm.de.benchmark.act.IAct;

/**
 * @author Sting
 *         <p>
 */
public class JMSProcesser {


    public static volatile boolean isRunning = false;
    public static int listenerPort = 8889;
    public static BKCode testType = null;
    public int messageLimit;
    public int timeSecLimit;

    ProcessContext ctx;

    private ServerSocket server = null;
    private BufferedReader request = null;
    private PrintWriter response = null;

    public static void main(String[] args) throws Exception {
        System.out.println("[processor]starting............");
        JMSProcesser worker = new JMSProcesser();
        worker.startListen();
    }

    public JMSProcesser() {
        ctx = new ProcessContext();
        ctx.processor = this;
        try {
            startListen();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void startListen() throws Exception {
        isRunning = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    server = new ServerSocket(listenerPort);
                    System.out.println("[processor]socket bind at " + listenerPort);
                    Socket client = server.accept();
                    System.out.println("[processor]socket start!");
                    request = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    response = new PrintWriter(client.getOutputStream(), true);

                    ctx.response = response;
                    ctx.fromAgent = request;
                } catch (BindException b) {
                    System.err.println("socket port " + listenerPort + " is in use,change to " + ++listenerPort);
                    this.run();
                }catch (SocketException e) {
                    System.err.println(e.getMessage());
                } catch (IOException e) {
                    e.printStackTrace();
                    System.exit(-1);
                }

                while (isRunning) {
                    try {
                        String line = request.readLine();
                        Cmd cmd = JSON.parseObject(line, Cmd.class);
                        IAct actor = cmd.guessAct();
                        actor.exec(ctx);
                    } catch (IOException e) {
                        System.out.println("Read failed");
                        System.exit(-1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }

    public void commandLocal(String line) {
        Cmd cmd = JSON.parseObject(line, Cmd.class);
        IAct actor = cmd.guessAct();
        try {
            actor.exec(ctx);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void distory() {
        try {
            isRunning = false;
            if (response != null) {
            response.println("ack");
            response.flush();
            }
            System.out.println("Client triggered this server to shutdown.");
            if (request != null) {
            request.close();
            request.close();
            }
            server.close();
            System.exit(0);
        } catch (IOException e) {
            System.out.println("Could not close.");
            System.exit(-1);
        }

    }


}
